package com.ellricky.baselib.ui.order

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.RecyclerView
import com.andr.common.ui.fragment.BaseFragment
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.widget.refresh.RvRefreshListener
import com.ellricky.baselib.BaseConstant
import com.ellricky.baselib.R
import com.ellricky.baselib.cache.LocalCacheUtil
import com.ellricky.baselib.dialog.picker.EPickerUtil
import com.ellricky.baselib.entity.BaseData
import com.ellricky.baselib.entity.ClassConsumeRecord
import com.ellricky.baselib.http.HttpUtils
import com.ellricky.baselib.util.EAppUtil
import com.ellricky.baselib.util.NewEBaseCallback
import com.ellricky.baselib.util.NewEListCallBack2
import kotlinx.android.synthetic.main.base_fragment_class_consume_record_child.*

class ClassConsumeRecordChildFragment : BaseFragment<Void, BasePresenter<Void>>(),
    RvRefreshListener {

    private var paramMap: HashMap<String, String> = hashMapOf()
    private var dataSource: List<ClassConsumeRecord> = arrayListOf()
    private var ePickerUtil: EPickerUtil? = null
    private var refresh = false

    private val courseId by lazy {
        arguments?.getString(BaseConstant.KEY_ID, "") ?: ""
    }

    private val memberId by lazy {
        arguments?.getString(BaseConstant.KEY_MEMBER_ID, "") ?: ""
    }

    private val adapter by lazy {
        ClassConsumeRecordAdapter()
    }

    interface OnConsumeListener {
        fun onTitleChange(basicNum: Int, specialNum: Int, artNum: Int, giveNum: Int)
    }

    private var onListener: OnConsumeListener? = null

    companion object {
        fun newInstance(courseType: String, memberId: String): ClassConsumeRecordChildFragment {
            val fragment = ClassConsumeRecordChildFragment()
            val bundle = Bundle().apply {
                putString(BaseConstant.KEY_ID, courseType)
                putString(BaseConstant.KEY_MEMBER_ID, memberId)
            }
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun getContentLayout(): Int {
        return R.layout.base_fragment_class_consume_record_child
    }

    override fun initUIView(p0: View?) {
        rvView.init(this)

        ePickerUtil = EPickerUtil(requireActivity())

        initListener()

        loadDataOnInit()
    }

    private fun initListener() {

    }

    private fun loadDataOnInit() {
        rvView.loadContent(true)
    }

    private fun initParamMap() {
        paramMap["action"] = "memberCourseSub"
        paramMap["eid"] = LocalCacheUtil.getUid()
        paramMap["member_id"] = memberId
        paramMap["course_type"] = courseId
        paramMap["page"] = rvView.getCurPage().toString()
        paramMap["page_size"] = "40"
    }

    override fun setAdapter(rv: RecyclerView) {
        rv.adapter = adapter
    }

    override fun loadData(refresh: Boolean) {
        this.refresh = refresh
        val url = EAppUtil.getApiHost()
        initParamMap()
        val callback: NewEBaseCallback = getResponseCallback2()
        HttpUtils.getInstance().postFrom(url, paramMap, callback)
    }

    private fun getResponseCallback2(): NewEBaseCallback {
        return object : NewEListCallBack2<ClassConsumeRecord>(requireActivity()) {
            override fun onSuccess(
                data: MutableList<ClassConsumeRecord>,
                baseData: BaseData<ClassConsumeRecord>,
                msg: String?
            ) {
                dataSource = data
                rvView.onLoadFinish(data, refresh)
                onListener?.onTitleChange(
                    baseData.basic_num,
                    baseData.special_num,
                    baseData.art_num,
                    baseData.give_num
                )
            }
        }
    }

    fun setOnTitleChangeListener(listener: OnConsumeListener) {
        onListener = listener
    }


    override fun updateAdapterData(refresh: Boolean) {
        if (refresh) adapter.refresh(dataSource)
        else adapter.loadMore(dataSource)
    }
}